<template>
  <div>
    <el-card class="box-card" style="margin-top: 20px">
      <div slot="header" class="clearfix">
        <el-tabs class="tab" v-model="activeName">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visit"></el-tab-pane>
        </el-tabs>
        <div class="right">
          <span @click="setToday">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span class="year" @click="setYear">本年</span>
          <el-date-picker
            size="mini"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']"
            class="date"
            v-model="date"
            value-format="yyyy-MM-dd"
            style="width: 300px"
          >
          </el-date-picker>
        </div>
      </div>
      <div>
        <el-row :gutter="10">
          <el-col :span="18">
            <div class="charts" ref="charts"></div>
          </el-col>
          <el-col :span="6">
            <div class="right2">
              <h3 style="margin: 0">门店{{ titleType }}排名</h3>
              <ul v-if="titleType == '销售额'">
                <li v-for="(item,index) in listStat.orderRank" :key="index" >
                  <span :class="{'rindex':index < 3}"> {{item.no}} </span>
                  <span> {{item.name}} </span>
                  <span class="rValue"> {{item.money}} </span>
                </li>
              </ul>
              <ul v-else>
                <li v-for="(item,index) in listStat.userRank" :key="index" >
                  <span :class="{'rindex':index < 3}"> {{item.no}} </span>
                  <span> {{item.name}} </span>
                  <span class="rValue"> {{item.money}} </span>
                </li>
              </ul>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'
import {mapState} from "vuex";

export default {
  name: "Sale",
  data() {
    return {
      activeName: 'sale',
      charts: null,
      date:[]
    }
  },
  methods: {
    setYear(){
      const  start = dayjs().startOf('year').format('YYYY-MM-DD')
      const  end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setToday() {
      const day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    setMonth() {
      const  start = dayjs().startOf('month').format('YYYY-MM-DD')
      const  end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setWeek() {
      const  day1 = dayjs().startOf('week').format('YYYY-MM-DD')
      const  day7 = dayjs().endOf('week').format('YYYY-MM-DD')
      this.date = [day1, day7]
    }
  },
  mounted() {
    this.charts = echarts.init(this.$refs.charts)
    let option = {
      title: {
        text: '销售额趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: this.listStat.orderFullYearAxis,
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '40%',
          data: this.listStat.orderFullYear
        }
      ]
    };
    this.charts.setOption(option)
  },
  computed: {
    titleType() {
      return this.activeName == 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listStat:state => state.home.SaleData
    })
  },
  watch: {

    titleType() {
        this.charts.setOption({
          title: {
            text: this.titleType
          },
          xAxis:{
            data:this.titleType == '销售额' ? this.listStat.orderFullYearAxis:this.listStat.userFullYearAxis,
          },
          series: [
            {
              name: 'Direct',
              type: 'bar',
              barWidth: '40%',
              data: this.titleType == "销售额" ? this.listStat.orderFullYear : this.listStat.userFullYear,
            },
          ]
        });
    },
    listStat(){
      this.charts.setOption({
        title: {
          text: this.titleType
        },
        xAxis:{
          data:this.listStat.orderFullYearAxis
        },
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '40%',
            data: this.listStat.orderFullYear
          },
        ]
      });
    }
  }
}
</script>

<style scoped>
.rValue {
  float: right;
}

.right2 {
  margin-top: 0;
  margin-bottom: 0;
  margin-right: 130px;
}

.charts {
  width: 100%;
  height: 400px;
}

.clearfix {
  display: flex;
  justify-content: space-between;
  position: relative;
}

span {
  margin: 0px 15px;
}

.tab {
  width: 100%;
}

.right {
  position: absolute;
  right: 0;
}

.date {
  width: 200px;
}

.year {
  margin-right: 30px;
}

ul {
  list-style: none;
  width: 400px;
  height: 300px;
  padding: 0;

}

ul li {
  height: 8%;
  margin: 30px 0;

}

.rindex {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
  float: left;
}
</style>
